var DzqFriend = {
		trigger : null,
		initFriendWin:function(uid,destuid,$trigger){
			this.trigger = $trigger;
			var html = '<div id="error_msg" style="background: none repeat scroll 0 0 #DEF0FF;padding:4px;margin-bottom:5px;float:left;display:none;"></div><div style="clear:both;"/>'
					+'<p>输入验证消息：</p><div style="min-height: auto;" id="req_Friend" class="xgk">'
					
					+'<textarea style="overflow: auto;" aria-haspopup="true" class="xxm xbl" title="在这里输入请求内容" id="req_msg"></textarea>'
					+'</div><div class="xlw">'
					+'<a href="javascript:;" id="req_cancel" name="cancel" class="xfv">取消</a>'
					+'<a href="javascript:;" id="req_send" name="send" class="xe xpw">发送</a>'
					+'</div>';
			
			DianziQ.openWin({
					html:html,
					title:"发送好友请求",
					afterFun:function(){
						$("#req_cancel").bind("click",function(){
							DianziQ.closeWin();
						});
						$("#req_send").bind("click",function(){
							var msg = $("#req_msg").val();
							msg = $.trim(msg);
							
							if(DianziQ.realLength(msg) > 200){
								//console.log(DianziQ.realLength(msg));
								//DianziQ.alert("请求信息超过");
								var len = DianziQ.remainLen(msg,100,'cn');
								$("#error_msg").show().text("请求内容超出了"+(-len)+"个字符");
								return;
							}else{
								$("#error_msg").hide().text("");
							}
							
							DzqFriend.sendReqFriend(uid,destuid,msg);
						});
					}
				});
			
		},
		sendReqFriend:function(uid,destuid,msg){
			if(isSubmit)
				return;
				
			DianziQ.post(Constans.BASE_PATH+"relation/friend!sendReqFriend.action",{uid:uid,destuid:destuid,msg:msg},function(data){
				if(data.messages == "0"){
					if(DzqFriend.trigger.attr("data-area")){
						$("#del_friend").hide();
						$("#add_friend").hide();
						$("#req_friend").show();
						DianziQ.closeWin();
						DianziQ.alert("好友请求已经发送");
						DzqFriend.remFriend("#bubble a[data-name='rem-friend']");
					}else{
						DianziQ.closeWin();
						DianziQ.alert("好友请求已经发送");
						var $div = DzqFriend.trigger.parent();
						if($div.attr("class").indexOf("green_btn") >= 0){
							$div.removeClass("green_btn");
							$div.addClass("grey_btn");
						}else{
							$div.removeClass("b_add_friend");
							$div.addClass("b_attented");
						}
						$div.html('已发好友请求');
						$div.unbind("click");
					}
					//DzqFriend.trigger.parents("div.name").text("已发请求");
					//DzqFriend.trigger.
				}else if(data.messages == "2"){
					DianziQ.alert("你已经向他发送过好友请求信息");
				}else if(data.messages == "3"){
					DianziQ.alert("不能给自己发送好友请求");
				}else{
					DianziQ.alert("系统繁忙请稍后再试");
				}
			});
		},
		removeFriend:function(uid,destuid){
			if(isSubmit)
				return ;
				
			DianziQ.post(Constans.BASE_PATH+"relation/friend!removeFriend.action",{uid:uid,destuid:destuid},function(data){
				if(data.messages == "0"){
					if(DzqFriend.trigger.attr("data-area")){
						$("#del_friend").hide();
						$("#add_friend").show();
						$("#req_friend").hide();
						DianziQ.closeWin();
						DianziQ.alert("删除成功");
						DzqFriend.reqFriend("#bubble a[data-name='req-friend']");
					}else{
						DianziQ.closeWin();
						DianziQ.alert("删除成功");
						var $div = DzqFriend.trigger.parent();
						if($div.attr("class").indexOf("grey_btn") >= 0){
							var _html = '<a title="加好友" href="javascript:;" data-uid="'+destuid+'" data-name="req-friend"><strong>+</strong>&nbsp;&nbsp;加好友</a>';
							$div.removeClass("grey_btn");
							$div.addClass("green_btn");
							$div.html(_html);
						}else{
							$div.removeClass("b_attented");
							$div.addClass("b_add_friend");
							//DzqFriend.trigger.parent().hide();
							$div.html('<a href="javascript:;" data-name="req-friend" data-uid="'+destuid+'">加好友</a>');
							DzqFriend.trigger.parent().remove();
						}
						DzqFriend.reqFriend("a[data-name='req-friend']");
					}
					
				}else{
					DianziQ.alert("系统繁忙请稍后再试");
				}
			});
		},
		reqFriend:function(rule){
			$(rule).parent().unbind("click");
			
			$(rule).parent().bind("click",function(){
				if(DZQCheckForUser())
					return;
				if(!checkAuth(DZQ_ACTIONS.REQ_FRIEND)){
					return;
				}
				
				
				var destuid = $(this).children().attr("data-uid");
				var $trigger = $(this).children();
				DzqFriend.initFriendWin(User.id,destuid,$trigger);
			});
		},
		remFriend:function(rule){
			$(rule).unbind("click");
			
			$(rule).bind("click",function(){
				DzqFriend.trigger = $(this);
				var destuid = $(this).attr("data-uid");
				DianziQ.alert("确定要删除好友吗？","",{"确定":function(){
					DzqFriend.removeFriend(User.id,destuid);
						$( this ).dialog( "close" );
					},"取消":function(){
						$( this ).dialog( "close" );
					}});
			});
		}
};

$(function(){
	DzqFriend.reqFriend("a[data-name='req-friend']");
	DzqFriend.remFriend("a[data-name='rem-friend']");
});